<template>
  <div class="wrapper">
    <div style="margin: 200px auto; background-color: #fff; width: 350px; height: 340px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>注 册</b></div>
      <!-- 1.:rules="rules"  2. <el-form-item prop="username"> 3.    4:model="user" -->
      <el-form :model="mUser" :rules="rules" ref="userForm">
        <el-form-item prop="account" style="width: 250px;margin: 25px 50px;">
          <el-input placeholder="请输入用户名" size="default"  prefix-icon="User" v-model="mUser.account"></el-input>
        </el-form-item>
        <el-form-item prop="phone" style="width: 250px;margin: 25px 50px;">
          <el-input placeholder="请输入电话" size="default"  prefix-icon="User" v-model="mUser.phone"></el-input>
        </el-form-item>
        <el-form-item prop="password" style="width: 250px;margin: 25px 50px;">
          <el-input placeholder="请输入密码" size="default"  prefix-icon="Lock" show-password v-model="mUser.password"></el-input>
        </el-form-item>
        <el-form-item prop="password2" style="width: 250px;margin: 25px 50px;">
          <el-input placeholder="请确认密码" size="default"  prefix-icon="Lock" show-password v-model="mUser.password2"></el-input>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-button type="primary" size="small"  autocomplete="off" @click="register11">注册</el-button>
          <el-button type="warning" size="small"  autocomplete="off" @click="toLogin">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      mUser: {},
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 10, max: 15, message: '长度在 10 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    register() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {  // 表单校验合法
        if (this.mUser.password !== this.mUser.password2) {
            this.$message.error("两次输入的密码不一致")
            return false
          }
          // 准备JSON
          let a = {
            phone:this.mUser.phone,
            account:this.mUser.account,
            password:this.mUser.password2
          }
          this.request.post("/merchants/add", JSON.stringify(a)).then(res => {
              //console.log(res);
           if(res.code === 200) {
             this.$message.success("注册成功！")
              this.$router.push("/login")
              
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });
    },
    toLogin(){
      this.$router.push("/login")
    },
  }
}
</script>

<style>
  .wrapper {
    height: 100vh;
    background-image: linear-gradient(to bottom right, #FC466B , #3F5EFB);
    overflow: hidden;
  }
</style>